// 获取路由详情信息
import { cloneDeep } from 'lodash'
import  { MainRoutes } from '../router/index'
import { RouterType } from '../types/router'
import { useMain } from '../store/main'
export const RoutesObj = () => {
    let obj: { [index: string]: string } = {}
    function loop(arr: RouterType[]) {
        arr.forEach((item: RouterType) => {
            obj[item.path] = item.meta.label
            if (item.children) {
                loop(item.children)
            }
        })
    }
    // loop([...MainRoutes])
    loop(MainRoutes)
    return obj
}
// 转换时间
export const handleDate = (date: any) => {
    let dateTime = ''
    if (typeof (date) === 'object') {
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        // let h = date.getHours()
        // h = h < 10 ? ('0' + h) : h
        // let M = date.getMinutes()
        // M = M < 10 ? ('0' + M) : M
        // let s = date.getSeconds()
        // s = s < 10 ? ('0' + s) : s
        dateTime = y + '-' + m + '-' + d //+ ' ' + h + ':' + M + ':' + s;
    }
    return dateTime
}
export const Navigation = (arr: RouterType[]) => {
    arr.forEach((item: RouterType, index) => {
        if (item.path == "/students/details/:id/:name") {
            arr.splice(index, 1)
        }
        if (item.children) {
            item.children.forEach((child, index) => {
                if (child.path == "/students/details/:id/:name") {
                    item.children?.splice(index, 1)
                }
            })
        }
        return arr
    })
}


// 将路由数据处理为 path:label格式数据

// export const routesToMap = () => {
//     let obj = {};
//     function loop(arr) {
//         arr.forEach((item) => {
//             obj[item.path] = item.meta.label;
//             if (item.children) {
//                 loop(item.children);
//             }
//         });
//     }
//     loop(mainRoutes);
//     return obj;
// };

// 根据权限删减路由数据表

export const filterRoutes = (permission: string[]) => {
    function loop(arr: RouterType[]) {
        for (let i = arr.length - 1; i >= 0; i--) {
            let perArr = permission.filter((permit) => permit.indexOf(arr[i].path) != -1)//！=-1表示找到了
            if (!perArr.length) {
                arr.splice(i, 1)
            }
            if (arr[i] && arr[i].children) {
                loop(arr[i].children!)
            }
        }
    }
    let main=useMain()
    console.log(main.tabsIdx,'状态机');
    let routes = cloneDeep(MainRoutes)
    if(main.tabsIdx==0){
        routes=routes.splice(0,4)
    }else if(main.tabsIdx==1){
        routes.splice(1,5)
        routes=routes.splice(0,2)
    }else{
        routes.splice(1,3)
        routes=routes.splice(0,5)
    }
    console.log(routes);
    loop(routes)
    return routes
}